using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE80;
using EnvDTE;
using System.Reflection;

namespace ToolWindows
{
    public class Controller
    {
        private Window toolBrokersWindow = null;
        private Window infrastructureBrokersWindow = null;
        private Window allTheRestWindow = null;

        public const string brokersWindowCaption = "Brokers";
        public const string infrastructureWindowCaption = "Service Broker infrastructure";
        public const string allTherestWindowCaption = "All the rest...";

        public Window BrokersWindow
        {
            get
            {
                return toolBrokersWindow;
            }
        }

        public Window InfrastructureWindow
        {
            get
            {
                return infrastructureBrokersWindow;
            }
        }

        public Window AllTheRestWindow
        {
            get
            {
                return allTheRestWindow;
            }
        }

        private static Controller currentInstance = new Controller();

        public static Controller Current
        {
            get
            {
                return currentInstance;
            }
        }

        private Window CreateToolWindow(    string typeName, 
                                            string assemblyLocation, 
                                            Guid uiTypeGuid, 
                                            DTE applicationObject, 
                                            AddIn addinInstance, 
                                            string caption)
        {
            Windows2 win2 = applicationObject.Windows as Windows2;

            if (win2 != null)
            {
                object controlObject = null;
                Window toolWindow = win2.CreateToolWindow2( addinInstance, 
                                                            assemblyLocation, 
                                                            typeName, 
                                                            caption, 
                                                            "{" + uiTypeGuid.ToString() + "}", 
                                                            ref controlObject);
                toolWindow.Visible = true;
                return toolWindow;
            }
            return null;
        }

        public void CreateBrokersWindow(DTE application, AddIn addinInstance)
        {
            if (toolBrokersWindow == null)
            {
                Assembly asm = Assembly.Load("ToolWindows");
                Guid id = Guid.NewGuid();

                toolBrokersWindow = CreateToolWindow(   "ToolWindows.Brokers", 
                                                        asm.Location, 
                                                        id, 
                                                        application, 
                                                        addinInstance, 
                                                        brokersWindowCaption);

                toolBrokersWindow.Height = 400;
                toolBrokersWindow.IsFloating = false;
                toolBrokersWindow.SetTabPicture(ToolsResource.Windows.GetHbitmap());
                application.MainWindow.LinkedWindows.Add(toolBrokersWindow);
                application.MainWindow.LinkedWindows.Item(brokersWindowCaption).AutoHides = true;
            }
        }

        public void CreateInfrastructureWindow(DTE application, AddIn addinInstance)
        {
            if (infrastructureBrokersWindow == null)
            {

                Assembly asm = Assembly.Load("ToolWindows");
                Guid id = Guid.NewGuid();
                infrastructureBrokersWindow = CreateToolWindow("ToolWindows.Infrastructure", asm.Location, id, application, addinInstance, infrastructureWindowCaption);

                infrastructureBrokersWindow.Height = 800;
                infrastructureBrokersWindow.IsFloating = false;
                infrastructureBrokersWindow.SetTabPicture(ToolsResource.Relationships.GetHbitmap());
                application.MainWindow.LinkedWindows.Add(infrastructureBrokersWindow);
                application.MainWindow.LinkedWindows.Item(infrastructureWindowCaption).AutoHides = true;

            }
        }

        public void CreateAllTheRestWindow(DTE application, AddIn addinInstance)
        {
            if (allTheRestWindow == null)
            {
                Assembly asm = Assembly.Load("ToolWindows");
                Guid id = Guid.NewGuid();
                allTheRestWindow = CreateToolWindow("ToolWindows.AllTheRest", asm.Location, id, application, addinInstance, allTherestWindowCaption);

                allTheRestWindow.Height = 1200;
                allTheRestWindow.IsFloating = false;
                allTheRestWindow.SetTabPicture(ToolsResource.SychronizeList.GetHbitmap());
                application.MainWindow.LinkedWindows.Add(allTheRestWindow);
                application.MainWindow.LinkedWindows.Item(allTheRestWindow).AutoHides = true;
            }
        }
    }
}
